package com.sunshine.lnuplus.utils

import android.content.*
import android.net.Uri
import androidx.fragment.app.FragmentManager
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.model.InitData
import com.sunshine.lnuplus.model.LessonBean
import com.sunshine.lnuplus.model.room.Lesson
import com.sunshine.lnuplus.model.room.TimetableRepository
import com.sunshine.lnuplus.view.DialogWithoutView
import es.dmoral.toasty.Toasty
import java.lang.Exception

object LessonUtils {
    fun initLessonBean(
        lessonList: List<Lesson>,
        context: Context?,
        fragmentManager: FragmentManager?,
        repository: TimetableRepository?) : Boolean{
        val lessonBeanList = ArrayList<LessonBean>()

        fun weeksToInt(weeks: String): ArrayList<Int> {
            val weekListInt = ArrayList<Int>()
            if(weeks.split("-").size == 2){
                for(week in weeks.split("-")[0].toInt() .. weeks.split("-")[1].toInt()){
                    weekListInt.add(week)
                }
            }else {
                weeks.replace(" ","").split(",").forEach {
                    weekListInt.add(it.toInt())
                }
            }
            return weekListInt
        }

        try {
            lessonList.forEach {
                val lessonBean = LessonBean(
                    lessonId = it.lessonId,
                    lessonName = it.lessonName,
                    dayOfWeek = it.dayOfWeek,
                    start = it.start,
                    end = it.end,
                    location = it.location,
                    teacher = it.teacher,
                    color = it.color,
                    weeks = weeksToInt(it.weeks)
                )
                lessonBeanList.add(lessonBean)
            }
            InitData.lessonList = lessonBeanList
            return true
        }catch (e: Exception){
            var string = "错误信息：$e\n"
            lessonList.forEach {
                string +=
                    it.lessonId+
                            ",name:"+
                            it.lessonName +
                            ",dayofweek:"+
                            it.dayOfWeek +
                            ",start:"+
                            it.start +
                            ",end:"+
                            it.end +
                            ",location:"+
                            it.location +
                            ",teacher:"+
                            it.teacher +
                            ",color:"+
                            it.color +
                            ",weeks:"+
                            it.weeks +
                            "\n"
            }
            if(context != null && fragmentManager != null){
                TellMeDialog.tellMe(
                    context,
                    fragmentManager,
                    true,
                    "${context.resources.getString(R.string.tell_me)}${string}",
                    repository)
            }

            return false
        }
    }
}